package cn.holy.app.xcddt.ui.activity.archive

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.core.content.ContextCompat
import cn.holy.app.xcddt.R
import cn.holy.app.xcddt.app.App
import cn.holy.app.xcddt.ui.activity.base.BaseActivity
import cn.holy.app.xcddt.ui.fragment.ArchiveAllFragment
import cn.holy.app.xcddt.ui.fragment.ArchiveFragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import kotlinx.android.synthetic.main.activity_archive.*

/**
 * Created by xionghao on 2018/3/1.
 */
@Route(path = "/app/archives")
class ArchiveActivity : BaseActivity() {

    private var pageAdapter: TabPager? = null
    private val titles = arrayOf("总览", "待派遣", "已派遣", "解决中", "已解决", "已结案")
    override fun initData() {
        tab.setupWithViewPager(viewpager)
        var index = 0
        titles.forEach { title ->
            var fragment: Fragment? = null
            if (titles.indexOf(title) == 0) {
                fragment = ArchiveAllFragment()

            } else {
                fragment = ArchiveFragment()
            }
            val bundle = Bundle()
            bundle.putInt("status", titles.indexOf(title) - 1)
            fragment.arguments = bundle
            pageAdapter?.addFragment(fragment, title)
            index++
        }
        viewpager.offscreenPageLimit = titles.size
        pageAdapter?.notifyDataSetChanged()

    }

    override fun initView() {
        App.appComponent.inject(this)
        QMUIStatusBarHelper.translucent(this)
        QMUIStatusBarHelper.setStatusBarDarkMode(this)
        top_bar.setTitle("案卷管理").setTextColor(ContextCompat.getColor(this, R.color.colorWhite))
        top_bar.addLeftImageButton(R.mipmap.ic_back, R.id.action_close).setOnClickListener({ finish() })
        pageAdapter = TabPager(supportFragmentManager)
        viewpager.adapter = pageAdapter
    }


    override fun initLayout() {
        setContentView(R.layout.activity_archive)
    }

}

class TabPager constructor(fm: FragmentManager) : FragmentPagerAdapter(fm) {

    private var fragments: ArrayList<Fragment> = ArrayList()
    private var titles: ArrayList<String> = ArrayList()
    override fun getItem(position: Int): Fragment {
        return fragments[position]
    }

    override fun getCount(): Int {
        return fragments.size
    }

    open fun addFragment(fragment: Fragment, title: String) {
        fragments.add(fragment)
        titles.add(title)
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return titles[position]
    }
}